package com.sansi.pencilbook.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 功能说明: 页面跳转
 *
 * @author lsy13
 */
@Slf4j
@Controller
public class PageController extends VerifyController implements BaseController{


    @GetMapping("index")
    public String index() {
        return "/pencilBookUser/index";
    }

    @GetMapping("login-register")
    public String loginRegister() {
        return "/pencilBookUser/login-register";
    }

    @GetMapping("my-account")
    public String myAccount(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/my-account";
        }
    }

    @GetMapping("adminIndex")
    public String adminIndex(HttpServletRequest request){
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/index";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("adminLogin")
    public String adminLogin(){
        return "/pencilBookAdmin/login";
    }

    @GetMapping("crud")
    public String adminCrud(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/crud";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("category_tab")
    public String adminCategory(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/category_tab";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("user_tab")
    public String adminUser(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/user_tab";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("address_tab")
    public String adminAddress(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/address_tab";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("registerStore")
    public String registerStore() {
        return "/pencilBookUser/checkout";
    }

    @GetMapping("bookDetails")
    public String bookDetails(@RequestParam("bookIsbn") String bookIsbn, HttpServletResponse response) {
        if (bookIsbn != null && bookIsbn != "") {
            Cookie cookie = new Cookie("bookIsbn", bookIsbn);
            response.addCookie(cookie);
        }
        return "/pencilBookUser/blog-details-left-sidebar";
    }

    @GetMapping("myStore")
    public String myStore(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/faq";
        }
    }

    @GetMapping("addStoreBook")
    public String addStoreBook(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/addStoreBook";
        }
    }

    @GetMapping("addBook")
    public String addBook(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/addBook";
        }
    }

    @GetMapping("updateStoreBooksNumAndPrice")
    public String updateStoreBooksNumAndPrice(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/uploadFile";
        }
    }

    @GetMapping("addStoreBooks")
    public String addStoreBooks(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/uploadFile";
        }
    }

    @GetMapping("updateStoreBooks")
    public String updateStoreBooks(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/uploadFile";
        }
    }

    @GetMapping("storeInfo")
    public String storeInfo(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "/pencilBookUser/login-register";
        }else {
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return "/pencilBookUser/login-register";
            }
            return "/pencilBookUser/store_info";
        }
    }

    @GetMapping("bookList")
    public String bookList(@RequestParam("categoryName") String categoryName, HttpServletResponse response) {
//        Cookie cookie = new Cookie("categoryName", Objects.requireNonNullElse(categoryName, ""));
        Cookie cookie = new Cookie("categoryName", categoryName);
        response.addCookie(cookie);
        return "/pencilBookUser/blog-3-column";
    }

    @GetMapping("imgUpload")
    public String imgUpload(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/imgUpload";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("addBooks")
    public String addBooks(HttpServletRequest request) {
        if (checkAdmin(request)) {
            return "/pencilBookAdmin/uploadFile";
        }else {
            return adminLogin();
        }
    }

    @GetMapping("cart")
    public String cart(HttpServletRequest request) {
        if (checkUser(request)) {
            return "/pencilBookUser/cart";
        }else {
            return loginRegister();
        }
    }

    @GetMapping("purchase")
    public String purchase(@RequestParam("storeId") String storeId, @RequestParam("Isbn") String Isbn, HttpServletRequest request, HttpServletResponse response) {
        if (checkUser(request)) {
            Cookie cookie = new Cookie("storeId", storeId);
            Cookie cookie1 = new Cookie("Isbn", Isbn);
            response.addCookie(cookie);
            response.addCookie(cookie1);
            return "/pencilBookUser/single-product-sale";
        }else {
            return loginRegister();
        }
    }

    @GetMapping("collection")
    public String collection(HttpServletRequest request) {
        if (checkUser(request)) {
            return "/pencilBookUser/shop-list-fullwidth";
        }else {
            return loginRegister();
        }
    }

}
